package com.lovo.dept.dao;

import com.lovo.common.dao.BaseDao;
import com.lovo.common.model.DeptBean;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 部门管理的dao实现类
 * @author 白德柯
 *
 */
public class DeptDaoImpl extends BaseDao implements IDeptDao {
    @Override
    public List<DeptBean> findAll() {
        String sql = "SELECT * FROM dept ";
        this.setConnection();
        List<DeptBean> list = new ArrayList<>();
        try {
            ps = con.prepareStatement(sql);
            rs = ps.executeQuery();
            while(rs.next()){
                list.add(fill());
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            this.close();
        }
        return list;
    }
    private DeptBean fill() throws SQLException {
        DeptBean deptBean = new DeptBean();
        deptBean.setId(rs.getInt("id"));
        deptBean.setName(rs.getString("dept_name"));
        if(rs.getDate("create_date") != null){
            deptBean.setCreateDate(rs.getDate("create_date").toLocalDate());
        }
        deptBean.setInfo(rs.getString("info"));
        return deptBean;
    }

    public static void main(String[] args) {
        IDeptDao deptDao = new DeptDaoImpl();
        System.out.println(deptDao.findAll());
    }
}
